
export async function execute_cmd(client_id, cmd_text) {
    let args = cmd_text.split(" ");
    let gm_function = functions[args[0]];
    if (gm_function !== undefined) {
        await gm_function(client_id, args);
    }
}

export const functions = {
    "@帮助": async (client_id, args) => {

        G.ChatUtils.send_system_chat(client_id,
            `
#R@添加帮派资金 #[帮派名] [资金] 
#R@添加银子 #[角色id] [数量] 
#R@添加仙玉 #[角色id] [数量] 
#R@添加经验 #[角色id] [数量] 
#R@添加物品 #[角色id] [名称] [数量] 
#R@添加召唤兽 #[角色id] [名称] 
#R@添加坐骑 #[角色id] [名称] 
#R@添加神器 #[角色id]
#R@保存数据
#R@清空背包
#R@加入战斗 #[战斗编号]
#R@获得所有灵宝 #[角色id]
#R@退出战斗
            `);
    },
    // @添加帮派资金 [帮派名] [资金]
    "@添加帮派资金": async (client_id, args) => {
        let name = args[1];
        let money = args[2];
        for (let [key, value] of _GangDataCache) {
            if (value["帮派名称"] === name) {
                G.GangUtils.add_gang_money(key, Number(money))
                ClientUtils.send_basic_prompt(client_id, "操作成功");
                break;
            }
        }
        ClientUtils.send_basic_prompt(client_id, "帮派不存在");
    },
    // @添加银子 [角色id] [数量]
    "@添加银子": async (client_id, args) => {
        let characters_id = Number(args[1]);
        let money = args[2];
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_number_field_value(characters_id, "银子", Number(money));
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @添加仙玉 [角色id] [数量]
    "@添加仙玉": async (client_id, args) => {
        let characters_id = Number(args[1]);
        let money = args[2];
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_number_field_value(characters_id, "仙玉", Number(money));
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @添加经验 [角色id] [数量]
    "@添加经验": async (client_id, args) => {
        let characters_id = Number(args[1]);
        let exp = args[2];
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_exp(characters_id, Number(exp));
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @添加物品 [角色id] [物品名称] [数量]
    "@添加物品": async (client_id, args) => {
        let characters_id = Number(args[1]);
        let item_name = args[2];
        let num = args[3];
        if (num === undefined) {
            num = 1;
        }
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_item(characters_id, item_name, Number(num));
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @添加召唤兽 [角色id] [召唤兽名称]
    "@添加召唤兽": async (client_id, args) => {
        let characters_id = Number(args[1]);
        let pet_name = args[2];
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_pet(characters_id, pet_name);
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @添加坐骑 [角色id] [坐骑名称]
    "@添加坐骑": async (client_id, args) => {
        let characters_id = Number(args[1]);
        let mounts_name = args[2];
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_mounts(characters_id, mounts_name);
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @添加神器 [角色id]
    "@添加神器": async (client_id, args) => {
        let characters_id = Number(args[1]);
        if (_CharactersData.has(characters_id)) {
            G.Characters.add_shenqi(characters_id);
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    // @保存数据
    "@保存数据": async (client_id, args) => {
        let username = _ClientUserNameData.get(client_id);
        let characters = G.Characters.get_current_login_characters(client_id);
        if (characters !== undefined) {
            //TODO 持久化所有在线角色的最新数据
            let all_characters = G.Characters.get_all_characters(username);
            for (let index = 0; index < all_characters.length; index++) {
                let character = all_characters[index];
                let offline_time = Math.floor(Date.now() / 1000);
                character["离线时间"] = offline_time;
                if (_GangDataCache.has(characters["BPID"])) {
                    _GangDataCache.get(characters["BPID"])["成员数据"][characters["id"]]["离线时间"] = offline_time;
                }
                await G.Characters.update_characters_data_by_json_data(character);
            }
        }
    },
    // @清空背包
    "@清空背包": async (client_id, args) => {
        let characters = G.Characters.get_current_login_characters(client_id);
        if (characters !== undefined) {
            characters["道具"] = [];
        }
        ClientUtils.send_basic_prompt(client_id, "操作成功");
    },
    // @加入战斗
    "@加入战斗": async (client_id, args) => {
        let fight_id = args[1];
        let characters = G.Characters.get_current_login_characters(client_id);
        G.FightUtils.create_monster_fight(characters["id"], fight_id, fight_id);
        ClientUtils.send_basic_prompt(client_id, "操作成功");
    },
    "@获得所有灵宝": async (client_id, args) => {
        let characters_id = Number(args[1]);
        if (_CharactersData.has(characters_id)) {
            for (let index = 0; index < G.ItemUtils.all_lingbao.length; index++) {
                let lb_name = G.ItemUtils.all_lingbao[index];
                G.Characters.add_item(characters_id, lb_name, 1);
            }
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    "@清空灵宝": async (client_id, args) => {
        let characters_id = Number(args[1]);
        if (_CharactersData.has(characters_id)) {
            let characters = _CharactersData.get(characters_id);
            characters["灵宝"] = [];
            characters["灵宝佩戴"] = [];
            G.Characters.refrush_fabao_data(characters);
            ClientUtils.send_basic_prompt(client_id, "操作成功");
        } else {
            ClientUtils.send_basic_prompt(client_id, "角色不存在或离线");
        }
    },
    "@退出战斗": async (client_id, args) => {
        let characters = G.Characters.get_current_login_characters(client_id);
        G.FightUtils.exit_fight(characters["战斗开关"]);
    },
}

